//
//  SPHttpClient.m
//  Sport
//
//  Created by Cass on 2018/4/3.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPHttpClient.h"
@interface SPHttpClient()
@property(nonatomic, strong) AFHTTPSessionManager *manager;

@end
@implementation SPHttpClient

- (AFHTTPSessionManager *)manager{
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
        _manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/json",@"text/javascript"@"text/plain", nil];
    }
    return _manager;
}
+ (instancetype)sharedHttpClient{
    static id _instace = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instace = [[self alloc]init];
    });
    return _instace;
}

+ (void)GET:(NSString *)URLString parameters:(id)parameters success:(successBlock)successBlock failure:(failureBlock)failureBlock{
    [[self sharedHttpClient]requestWithHttpMethod:SPHttpMethodGet urlString:URLString parameters:parameters fileData:nil progress:nil success:successBlock failure:failureBlock];
}

+ (void)POST:(NSString *)URLString parameters:(id)parameters success:(successBlock)successBlock failure:(failureBlock)failureBlock{
    [[self sharedHttpClient] requestWithHttpMethod:SPHttpMethodPost urlString:URLString parameters:parameters fileData:nil progress:nil success:successBlock failure:failureBlock];
}

+ (void)UPLOAD:(NSString *)URLString parameters:(id)parameters fileData:(NSData *)fileData progress:(progressBlock)progressBlock success:(successBlock)successBlock failure:(failureBlock)failureBlock{
    [[self sharedHttpClient]requestWithHttpMethod:SPHttpMethodUpload urlString:URLString parameters:parameters fileData:fileData progress:progressBlock success:successBlock failure:failureBlock];
}

- (void)requestWithHttpMethod:(SPHttpMethod )httpMethod urlString:(NSString *)URLString parameters:(id )parameters fileData:(NSData *)fileData progress:(progressBlock )progressBlock success:(successBlock )successBlock failure:(failureBlock )failureBlock{
    switch (httpMethod) {
        case SPHttpMethodGet:
            {
                [self.manager GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    successBlock(responseObject);
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    failureBlock(error);
                }];
            }
            break;
        case SPHttpMethodPost:
            {
                [self.manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    successBlock(responseObject);
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    failureBlock(error);
                }];
            }
            break;
        case SPHttpMethodUpload:{
            [self.manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
                //设置时间格式
                [formatter setDateFormat:@"yyyyMMddHHmmssSSS"];
                NSString *dateString = [formatter stringFromDate:[NSDate date]];
                NSString *fileName = [NSString stringWithFormat:@"%@.jpg",dateString];
                [formData appendPartWithFileData:fileData name:@"file[]" fileName:fileName mimeType:@"image/jpg"];
            } progress:^(NSProgress * _Nonnull uploadProgress) {
                progressBlock((uploadProgress.completedUnitCount * 1.0 / uploadProgress.totalUnitCount));
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                successBlock(responseObject);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                failureBlock(error);
            }];
        }
            break;
        default:
            break;
    }
}
@end
